package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"useryk/model/mysql"
	"useryk/model/redisinit"
	"useryk/userrpc/internal/svc"
	"useryk/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductLikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductLikeLogic {
	return &ProductLikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductLikeLogic) ProductLike(in *userrpc.ProductLikeRequest) (*userrpc.ProductLikeResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Product
	mysql.DB.Where("id = ?", in.Id).Find(&product)
	if product.ID == 0 {
		return nil, fmt.Errorf("该产品不存在无法收藏")
	}
	marshal, _ := json.Marshal(&product)
	redisinit.Rdb.ZIncrBy("liek", 1, string(marshal))

	return &userrpc.ProductLikeResponse{
		Id: int64(product.ID),
	}, nil
}
